<?php

require_once ROOT_SERVICES . DS . 'shipping_provider_interface.php';
require_once ROOT_LIBRARY_HTMLDOM . DS . 'simple_html_dom.php';

// 2GO
class shipping_provider_11 implements shipping_provider_interface {

    private $_shipment;

    public function __construct($shipment) {
        $this->_shipment = $shipment;
    }

    public function get_status_shipping() {
        try
        {
            $tracking_number = $this->_shipment ['tracking_number'];
            $str_link = isset($this->_shipment ['url']) ? $this->_shipment ['url'] : "http://supplychain.2go.com.ph/CustomerSupport/etrace/indiv1.asp?code=";
            $str_link .= $tracking_number;
            $html = file_get_html($str_link);
            $status_list = array();

            $table_content_e = $html->find('table.table-contents', 1);
            if (is_object($table_content_e)) {
                foreach ($table_content_e->find('tbody tr') as $tr) {
                    $tds = $tr->find('td[class^=table-contents-td]');
                    $time = array_shift($tds);
                    $status = array_shift($tds);
                    if ($time && $status) {
                        $check_time_m = preg_match("/(\d{1,2})\/(\d{1,2})\/(\d{4}) (\d{1,2}):(\d{1,2}):(\d{1,2})/", $time->plaintext, $matches);
                        if ($check_time_m) {
                            list( $date, $m, $j, $Y, $H, $i, $s ) = $matches;
                            $status_list[] = array(
                                'time' => "{$Y}-{$m}-{$j} {$H}:{$i}:{$s}",
                                'status' => "({$this->_shipment ['shipment_provider_name']}) " . $status->plaintext
                            );
                        }
                    }
                }
            }
            return array('sortable' => TRUE, 'status_list' => $status_list);
        } catch (Exception $e)
        {
            return array('sortable' => FALSE, 'status_list' => null);
        }
    }

}

?>
